
package cn.wjdiankong.levideo.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.douyin.data.DouyinVideoListData;
import cn.wjdiankong.levideo.douyin.utils.DouyinUtils;
import cn.wjdiankong.levideo.list.adapter.DouyinFragmentListAdapter;
import cn.wjdiankong.levideo.net.HttpClientUtils;
import cn.wjdiankong.levideo.net.HttpClientUtils.ResponseCallback;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.view.StaggeredGridView;

public class DouyinFragment extends BaseFragment{

	private static final String ARG_POSITION = "position";
	
	private long maxCursor, minCursor;
	
	public static DouyinFragment newInstance(int position, String title, int type) {
		DouyinFragment f = new DouyinFragment();
		f.title = title;
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		f.type = type;
		return f;
	}
	
	@SuppressLint({ "InlinedApi", "InflateParams", "NewApi" })
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout = super.onCreateView(inflater, container, savedInstanceState);
		mListView = (StaggeredGridView) layout.findViewById(R.id.grid_view);
		adapter = new DouyinFragmentListAdapter(getActivity(), mListView, isLocal);
		adapter.setListView(mListView);
		initListViewListener();
		if(!isLocal){
			showLoading();
			getBeforeListData();
		}else{
			if(adapter != null){
				adapter.setBeforeDataList(videoDataList);
			}
		}
		return layout;
	}

	public void getBeforeListData(){
		String url = DouyinUtils.getEncryptUrl(getActivity(), 0, 0);
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setResponseCallback(new ResponseCallback() {
			@Override
			public void responseFinish(final String result) {
				DouyinVideoListData listData = DouyinVideoListData.fromJSONData(result);
				loadDownDataFinish(listData.videoDataList);
				maxCursor = listData.maxCursor;
				minCursor = listData.minCursor;
			}
			
			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}

			@Override
			public void responseFinish(byte[] result) {
				
			}
		}).execGet();
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_douyin_layout;
	}

	@Override
	protected void getAfterListData() {
		boolean isMinCursor = (minCursor >= 0);
		String url = DouyinUtils.getEncryptUrl(getActivity(), isMinCursor ? minCursor : -1, isMinCursor ? -1 : maxCursor);
		HttpClientUtils httpClient = new HttpClientUtils();
		httpClient.setUrl(url).setResponseCallback(new ResponseCallback() {
			@Override
			public void responseFinish(final String result) {
				DouyinVideoListData listData = DouyinVideoListData.fromJSONData(result);
				loadUpDataFinish(listData.videoDataList);
				maxCursor = listData.maxCursor;
				minCursor = listData.minCursor;
			}
			
			@Override
			public void reponseFail() {
				Logger.log("get data err!");
				loadDataFailed();
			}

			@Override
			public void responseFinish(byte[] result) {
				
			}
		}).execGet();
		//请求一次之后就要设置-1了
		minCursor = -1;
	}
	
}